home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Dialog Boxes / ModelessColorScroll / ColorScrollDialogBox.cs next >
Encoding:
Text File  |  2001-01-15  |  3.3 KB  |  98 lines

  1. //---------------------------------------------------
  2. // ColorScrollDialogBox.cs ⌐ 2001 by Charles Petzold
  3. //---------------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7.  
  8. class ColorScrollDialogBox: Form
  9. {
  10.      Label[]      alabelName  = new Label[3];
  11.      Label[]      alabelValue = new Label[3];
  12.      VScrollBar[] avscroll    = new VScrollBar[3];
  13.  
  14.      public event EventHandler Changed;
  15.  
  16.      public ColorScrollDialogBox()
  17.      {
  18.           Text = "Color Scroll Dialog Box";
  19.  
  20.           ControlBox    = false;
  21.           MinimizeBox   = false;
  22.           MaximizeBox   = false;
  23.           ShowInTaskbar = false;
  24.  
  25.           Color[] acolor = { Color.Red, Color.Green, Color.Blue };
  26.  
  27.           for (int i = 0; i < 3; i++)
  28.           {
  29.                alabelName[i] = new Label();
  30.                alabelName[i].Parent = this;
  31.                alabelName[i].ForeColor = acolor[i];
  32.                alabelName[i].Text = "&" + acolor[i].ToKnownColor();
  33.                alabelName[i].TextAlign = ContentAlignment.MiddleCenter;
  34.  
  35.                avscroll[i] = new VScrollBar();
  36.                avscroll[i].Parent = this;
  37.                avscroll[i].SmallChange = 1;
  38.                avscroll[i].LargeChange = 16;
  39.                avscroll[i].Minimum  = 0;
  40.                avscroll[i].Maximum = 255 + avscroll[i].LargeChange - 1;
  41.                avscroll[i].ValueChanged += 
  42.                               new EventHandler(ScrollOnValueChanged);
  43.                avscroll[i].TabStop = true;
  44.  
  45.                alabelValue[i] = new Label();
  46.                alabelValue[i].Parent = this;
  47.                alabelValue[i].TextAlign = ContentAlignment.MiddleCenter;
  48.           }
  49.  
  50.           OnResize(EventArgs.Empty);
  51.      }
  52.      public Color Color
  53.      {
  54.           get 
  55.           { 
  56.                return Color.FromArgb(avscroll[0].Value,
  57.                                      avscroll[1].Value,
  58.                                      avscroll[2].Value); 
  59.           }
  60.           set 
  61.           {
  62.                avscroll[0].Value = value.R;
  63.                avscroll[1].Value = value.G;
  64.                avscroll[2].Value = value.B;
  65.           }
  66.      }
  67.      protected override void OnResize(EventArgs ea)
  68.      {
  69.           base.OnResize(ea);
  70.  
  71.           int cx = ClientSize.Width;
  72.           int cy = ClientSize.Height;
  73.           int cyFont = Font.Height;
  74.  
  75.           for (int i = 0; i < 3; i++)
  76.           {
  77.                alabelName[i].Location = new Point(i * cx / 3, cyFont / 2);
  78.                alabelName[i].Size = new Size(cx / 3, cyFont);
  79.  
  80.                avscroll[i].Location = new Point((4 * i + 1) * cx / 12,
  81.                                                 2 * cyFont);
  82.                avscroll[i].Size = new Size(cx / 6, cy - 4 * cyFont);
  83.  
  84.                alabelValue[i].Location = new Point(i * cx / 3,
  85.                                                    cy - 3 * cyFont / 2);
  86.                alabelValue[i].Size = new Size(cx / 3, cyFont);
  87.           }
  88.      }
  89.      void ScrollOnValueChanged(Object obj, EventArgs ea)
  90.      {
  91.           for (int i = 0; i < 3; i++)
  92.                if((VScrollBar) obj == avscroll[i])
  93.                     alabelValue[i].Text = avscroll[i].Value.ToString();
  94.  
  95.           if (Changed != null)
  96.                Changed(this, new EventArgs());
  97.      }
  98. }